package com.shankungfu.main;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.shankungfu.GlobleConstant;
import com.shankungfu.MainApplication;
import com.shankungfu.R;
import com.shankungfu.bean.BaseBean;
import com.shankungfu.util.MyAsyncHttpResponseHandler;
import com.shankungfu.util.ToastUtil;
import com.shankungfu.util.WebService;
import com.shankungfu.view.CustomDialog;
import com.shankungfu.view.CustomDialog.Builder;

public class ApplyActivity extends Activity implements OnClickListener{
	private static final int DATA_SUCCESS = 1;
	
	private Button confirm_btn;
	private Button cancel_btn;
	private EditText apply_name_et;
	private EditText apply_email_et;
	private EditText apply_phone_et;
	private EditText apply_words_et;
	private TextView detail_title;
	private ImageView head_refresh;
	private ImageView head_goback;
	private ImageView iv;
	private String project_id;
	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case DATA_SUCCESS:
				BaseBean baseBean = (BaseBean) msg.obj;
				if(baseBean.getMsg() != null && baseBean.getMsg().length() != 0){
					ToastUtil.showMessageText(ApplyActivity.this, baseBean.getMsg());
				} else {
					CustomDialog.Builder builder = new Builder(ApplyActivity.this);
					builder.setMessage("申请成功!");
					builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							ApplyActivity.this.finish();
						}
					});
					builder.create().show();
				}
				break;

			default:
				break;
			}
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.apply_layout);
		
		init();
		
	}

	private void init() {
		project_id = getIntent().getStringExtra("project_id");
		
		confirm_btn = (Button) findViewById(R.id.apply_confirm_btn);
		cancel_btn = (Button) findViewById(R.id.apply_cancel_btn);
		apply_name_et = (EditText) findViewById(R.id.apply_name);
		apply_email_et = (EditText) findViewById(R.id.apply_email);
		apply_phone_et = (EditText) findViewById(R.id.apply_phone);
		apply_words_et = (EditText) findViewById(R.id.apply_words);
		detail_title = (TextView) findViewById(R.id.detail_title);
		head_refresh = (ImageView) findViewById(R.id.head_refresh);
		head_goback = (ImageView) findViewById(R.id.head_goback);
		iv = (ImageView) findViewById(R.id.iv);
		
		head_refresh.setVisibility(View.GONE);
		iv.setVisibility(View.GONE);
		
		detail_title.setText("申请");
		if (MainApplication.userBean != null) {
			apply_name_et.setText(MainApplication.userBean.getLoginData().getUser_name());
			apply_email_et.setText(MainApplication.userBean.getLoginData().getEmail());
		}
		
		
		confirm_btn.setOnClickListener(this);
		cancel_btn.setOnClickListener(this);
		head_goback.setOnClickListener(this);
	}
	
	private void getData() {
		WebService httpclient = new WebService();
		RequestParams params = new RequestParams();
		params.add("serviceId", "project_join");
		params.add("app_info", "shankungfu");
		params.add("project_Id", project_id);
		params.add("user_Id", MainApplication.userBean.getLoginData().getUser_Id());
		params.add("user_name", MainApplication.userBean.getLoginData().getUser_name());
		params.add("email", MainApplication.userBean.getLoginData().getEmail());
		params.add("telephone", apply_phone_et.getText().toString().trim());
		params.add("words", apply_words_et.getText().toString().trim());
		
		AsyncHttpResponseHandler responseHandler = new MyAsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String content) {
				if (!TextUtils.isEmpty(content)) {
					BaseBean baseBean = JSON.parseObject(content,
							BaseBean.class);

					Message msg = new Message();
					msg.what = DATA_SUCCESS;
					msg.obj = baseBean;
					handler.sendMessage(msg);
				}
			}

			@Override
			public void onFailure(String content) {
			}
		};
		httpclient.request(GlobleConstant.ACTIVITY_JOIN, params,
				responseHandler);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.apply_confirm_btn:
			getData();
			break;
		case R.id.apply_cancel_btn:
		case R.id.head_goback:
			finish();
			break;

		default:
			break;
		}
	}
}
